package com.tms.app.tms.widget;

import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.media.AudioManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

import com.tms.app.tms.R;
import com.tms.app.tms.bean.NotionBean;
import com.tms.app.tms.ui.WayBillInfoAC;
import com.tms.app.tms.ui.home.MainActivity;
import com.tms.app.tms.ui.taskinfo.TaskInfoActivity;
import com.tms.app.tms.utils.SharePreferenceUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by hl on 2017/5/25.
 */

public class PopuDialog implements View.OnClickListener{
    private Context context;
    private  PopupWindow popupWindow;


    public   void showPopu(final Context context, View view ){
        showPopu(context,view,null,"",true,true,false);
    }
    public   void showPopu(final Context context, View view ,View.OnClickListener listener, String text4){
        showPopu(context,view,listener,text4,true,false,true);
    }
    public   void showPopu(final Context context, View view ,View.OnClickListener listener, String text4,boolean show1,boolean show3,boolean show4){
        this.context=context;
        View  layout = LayoutInflater.from(context).inflate(R.layout.layout_voice,null);
        if (popupWindow!=null&&popupWindow.isShowing())
            return;
        popupWindow = new PopupWindow(layout,300, LinearLayout.LayoutParams.WRAP_CONTENT);
        // popupWindow自适应大小 popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); // 设置PopupWindow的大小（宽度和高度）
        // 高度和宽度为屏幕的比例 popupWindow.setWidth(display.getWidth() * 9 / 10); popupWindow.setHeight(display.getHeight() * 9 / 10);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        popupWindow.showAsDropDown(view);
       // popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());
        View layoutParent1=layout.findViewById(R.id.layout_1);
        View layoutParent2=layout.findViewById(R.id.layout_2);
        View layoutParent3=layout.findViewById(R.id.layout_3);
        View layoutParent4=layout.findViewById(R.id.layout_4);

        layoutParent1.setOnClickListener(this);
        layoutParent2.setOnClickListener(this);
        layoutParent3.setOnClickListener(this);
        layoutParent4.setOnClickListener(this);

        TextView textViewRight1=layout.findViewById(R.id.text_right_1);
        TextView textViewTop4=layout.findViewById(R.id.pop_photo_text_top_4);
        if (show1){
            layoutParent1.setVisibility(View.VISIBLE);
        }else {
            layoutParent1.setVisibility(View.GONE);
        }
        int numNo=SharePreferenceUtil.getInt(context,"tzhi",0);
        if (numNo>0){
           textViewRight1.setText(numNo+"");
           textViewRight1.setVisibility(View.VISIBLE);
        }
        else {
            textViewRight1.setVisibility(View.INVISIBLE);
        }
        if (show3){
            layoutParent3.setVisibility(View.VISIBLE);
        }else {
            layoutParent3.setVisibility(View.GONE);
        }

        if (text4!=null){
            textViewTop4.setText(text4);
        }
        if (listener!=null){
            layoutParent4.setOnClickListener(listener);
        }
        if (show4){
            layoutParent4.setVisibility(View.VISIBLE);
        }else {
            layoutParent4.setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(View v) {
        Intent intent=null;
        switch (v.getId()){
            case R.id.layout_1:
                intent=new Intent(context,MainActivity.class);
                intent.putExtra("showNav",1);
                context.startActivity(intent);
                popupWindow.dismiss();
                break;
            case R.id.layout_2:
                intent=new Intent(context,MainActivity.class);
                intent.putExtra("showNav",0);
                context.startActivity(intent);
                popupWindow.dismiss();
                break;
            case R.id.layout_3:
                int planId=SharePreferenceUtil.getInt(context,"planId",-1);
                intent=new Intent(context,TaskInfoActivity.class);
                intent.putExtra("planId",planId);
                context.startActivity(intent);
                popupWindow.dismiss();
                break;
            case R.id.layout_4:
                intent=new Intent(context,WayBillInfoAC.class);
                intent.putExtra("showNav",0);
                context.startActivity(intent);
                popupWindow.dismiss();
                break;

        }
    }
    public boolean isShowing(){
        if (popupWindow==null)
            return false;
        return popupWindow.isShowing();
    }

    public void close(){
        if (popupWindow!=null) {
            if (isShowing())
                popupWindow.dismiss();
            popupWindow=null;
        }
    }
}
